package com.example.demo.common.base;
public class JsonResult {

    /**
     * 响应消息
     */
    private String msg;

    /**
     * 响应中的数据
     */
    private Object data;

    /**
     * 响应中的业务状态
     */
    private Integer code;

    private JsonResult() {}

    /**
     * 成功，不带返回数据
     * @return JsonResult
     */
    public static JsonResult success(){
        JsonResult result = new JsonResult();
        result.setResultCode(ResultCode.SUCCESS);
        return result;
    }

    /**
     * 成功，携带返回数据
     * @param data 要返回给用户的数据
     * @return JsonResult
     */
    public static JsonResult success(Object data){
        JsonResult result = new JsonResult();
        result.setResultCode(ResultCode.SUCCESS);
        result.setData(data);
        return result;
    }

    /**
     * 失败，携带错误信息
     * @param resultCode ResultCode
     * @return JsonResult
     */
    public static JsonResult failure(ResultCode resultCode) {
        JsonResult result = new JsonResult();
        result.setResultCode(resultCode);
        return result;
    }

    /**
     * 设置响应码和对应的信息
     * @param code ResultCode
     */
    private void setResultCode(ResultCode code) {
        this.code = code.code();
        this.msg = code.message();
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }
}